package com.yaorange.certificate.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2025/5/6
 */
@Slf4j
public class QiniuUtils {
    private static final String accessKey = "1UgMlNCxScHDpT63zvPWNtCsO1-WS8DePWPbg6VJ";
    private static final String secretKey = "OSkbi938YBNmi7BWOTOeiZDm6YDyIpL93Zb8-HI8";
    private static final String bucket = "njt48";
    public static void upload2Qiniu(byte[] bytes, String originalFilename) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
           uploadManager.put(bytes, originalFilename, upToken);
        } catch (QiniuException ex) {
            log.error("上传到七牛云异常:{}",ex.getMessage());
        }
    }
}
